package com.china08.yxyapi.rabbitmq.listener;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.config.RabbitConfig;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnow;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;
import com.china08.yxyapi.util.DateUtils;

@Component
@EnableRabbit
public class HuixuePlanListener {

    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @Autowired
    private YxyHuixuePlanKnowRepository yxyHuixuePlanKnowRepository;

    // 考试小题数据分析
    @RabbitListener(queues = RabbitConfig.YW_HUIXUE_PLAN_CHECK_DAY)
    public void examQuesReport(Integer day) throws ParseException {
        List<YxyHuixuePlan> planLst = yxyHuixuePlanRepository.findByStatusAndEndDateLessThan(1, day);
        if (planLst.size() > 0) {
            for (YxyHuixuePlan yxyHuixuePlan : planLst) {
                if (yxyHuixuePlan.getProgress() >= 100) {
                    yxyHuixuePlan.setNotice("恭喜您，完成了本次学习计划！");
                    yxyHuixuePlan.setIconStatus(4);
                } else {
                    yxyHuixuePlan.setNotice("很遗憾，你没有完成既定的学习计划。不要气馁，重新开始吧！");
                    yxyHuixuePlan.setIconStatus(5);
                }
                yxyHuixuePlan.setStatus(0);
                yxyHuixuePlan.setLastModifiedDate(new Timestamp(DateUtils.formatStrDate4(day).getTime()));
                yxyHuixuePlanRepository.save(yxyHuixuePlan);
            }
        }

        // 正在进行中的计划
        List<YxyHuixuePlan> planIngLst = yxyHuixuePlanRepository.findByStatus(1);
        if (planIngLst.size() > 0) {
            for (YxyHuixuePlan yxyHuixuePlan : planIngLst) {
                // 计划之内
                double normalProgress = 0d;
                // 超出计划
                double nextProgress = 0d;
                Integer totalDay = (int) DateUtils.difference(DateUtils.formatStrDate4(yxyHuixuePlan.getBeginDate()),
                        DateUtils.formatStrDate4(yxyHuixuePlan.getEndDate())) + 1;
                Integer nowDay = (int) DateUtils.difference(DateUtils.formatStrDate4(yxyHuixuePlan.getBeginDate()),
                        DateUtils.formatStrDate4(day));
                normalProgress = nowDay * 1d / totalDay * 100;
                nextProgress = (nowDay + 1) * 1d / totalDay * 100;
                if (normalProgress > yxyHuixuePlan.getProgress()) {
                    yxyHuixuePlan.setNotice("您的学习进度已经落后，调整心态，胜利就在不远处！");
                    yxyHuixuePlan.setIconStatus(1);
                } else if (normalProgress < yxyHuixuePlan.getProgress() && nextProgress > yxyHuixuePlan.getProgress()) {
                    yxyHuixuePlan.setNotice("您的学习计划正在有条不紊的进行中，继续努力！");
                    yxyHuixuePlan.setIconStatus(2);
                } else if (nextProgress < yxyHuixuePlan.getProgress()) {
                    yxyHuixuePlan.setNotice("太令人吃惊了！您现在的学习进度已经超过了计划。注意劳逸结合，加油！");
                    yxyHuixuePlan.setIconStatus(3);
                } else if (yxyHuixuePlan.getProgress() >= 100) {
                    yxyHuixuePlan.setNotice("恭喜你，已经完成了既定的学习计划！不要骄傲，温故可以知新哦！");
                    yxyHuixuePlan.setIconStatus(4);
                }
                yxyHuixuePlan.setLastModifiedDate(new Timestamp(DateUtils.formatStrDate4(day).getTime()));
                yxyHuixuePlanRepository.save(yxyHuixuePlan);
            }
        }
    }

    // 学习计划掌握度计算
    @RabbitListener(queues = RabbitConfig.YW_HUIXUE_PLAN_CALCULATE_PROGRESS)
    public void calculateProgress(String planId) throws ParseException {

        YxyHuixuePlan plan = yxyHuixuePlanRepository.findById(planId);
        if (plan != null) {
            List<YxyHuixuePlanKnow> planKnowLst = yxyHuixuePlanKnowRepository.findByPlanIdOrderByOrderNumAsc(planId);
            double total = 0d, finish = 0d;
            for (YxyHuixuePlanKnow planKnow : planKnowLst) {
                finish += planKnow.getProgress();
            }
            total = planKnowLst.size() * 100;
            if (total != 0d) {
                BigDecimal bd = new BigDecimal(finish * 1d / total * 100);
                plan.setProgress(bd.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
                yxyHuixuePlanRepository.save(plan);
            }

            // 计划之内
            double normalProgress = 0d;
            // 超出计划
            double nextProgress = 0d;
            Integer totalDay = (int) DateUtils.difference(DateUtils.formatStrDate4(plan.getBeginDate()),
                    DateUtils.formatStrDate4(plan.getEndDate())) + 1;
            Integer nowDay = (int) DateUtils.difference(DateUtils.formatStrDate4(plan.getBeginDate()),
                    DateUtils.formatStrDate4(DateUtils.formatDate4(new Date())));
            normalProgress = nowDay * 1d / totalDay * 100;
            nextProgress = (nowDay + 1) * 1d / totalDay * 100;
            if (normalProgress > plan.getProgress()) {
                plan.setNotice("您的学习进度已经落后，调整心态，胜利就在不远处！");
                plan.setIconStatus(1);
            } else if (normalProgress < plan.getProgress() && nextProgress > plan.getProgress()) {
                plan.setNotice("您的学习计划正在有条不紊的进行中，继续努力！");
                plan.setIconStatus(2);
            } else if (nextProgress < plan.getProgress()) {
                plan.setNotice("太令人吃惊了！您现在的学习进度已经超过了计划。注意劳逸结合，加油！");
                plan.setIconStatus(3);
            } else if (plan.getProgress() >= 100) {
                plan.setNotice("恭喜你，已经完成了既定的学习计划！不要骄傲，温故可以知新哦！");
                plan.setIconStatus(4);
            }
            plan.setLastModifiedDate(new Timestamp(new Date().getTime()));
            yxyHuixuePlanRepository.save(plan);
        }
    }
}
